package com.turing.post.organization.impl;

import java.math.BigInteger;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.organization.util.Voucher;
import com.turing.post.organization.util.VoucherDao;
import com.turing.post.organization.util.VoucherInStockOrderDetail;
import com.turing.post.organization.util.VoucherInStockOrderDetailBusiness;
import com.turing.post.organization.util.VoucherInStockOrderDetailDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author wzd
 * @Dec 18, 2012
 */
@Stateless(mappedName = "organization.impl.VoucherInStockOrderDetailBusinessImpl")
@Remote
@Local
public class VoucherInStockOrderDetailBusinessImpl implements
		VoucherInStockOrderDetailBusiness {
	@EJB(mappedName = "organization.impl.VoucherInStockOrderDetailDaoImpl")
	private VoucherInStockOrderDetailDao voucherInStockOrderDetailDaoImpl;// 凭证入库单明细

	@EJB(mappedName = "organization.impl.VoucherDaoImpl")
	private VoucherDao voucherDaoImpl;// 凭证种类

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;// 日志

	public void insertVoucherInStockOrderDetail(PCPSWebInput webInput,
			VoucherInStockOrderDetail voucherInStockOrderDetail)
			throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 入库单Id
		if (voucherInStockOrderDetail.getVoucherInStockOrderId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"入库单ID为空!");
		}
		// 顺序号
		if (voucherInStockOrderDetail.getSeqNo() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"顺序号为空!");
		}
		// 凭证种类ID
		if (voucherInStockOrderDetail.getVoucherId() == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证种类ID不能等于0!");
		}
		// 起始凭证号
		if (PCPSUtil.isEmpty(voucherInStockOrderDetail.getStartVoucherNo())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"起始凭证号为空!");
		}
		// 终止凭证号
		if (PCPSUtil.isEmpty(voucherInStockOrderDetail.getEndVoucherNo())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"终止凭证号为空!");
		}
		// 验证凭证号长度是否合法
		Voucher voucher = voucherDaoImpl
				.getVoucherByVoucherId(voucherInStockOrderDetail.getVoucherId());
		if (voucher == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"凭证种类不合法!");
		} else {
			if (voucher.getVoucherNoLen() == new BigInteger(String
					.valueOf(voucherInStockOrderDetail.getStartVoucherNo()
							.length()))) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "起始凭证号长度为:" + voucher.getVoucherNoLen().toString());
			}
			if (voucher.getVoucherNoLen() == new BigInteger(String
					.valueOf(voucherInStockOrderDetail.getEndVoucherNo()
							.length()))) {
				throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
						"", "终止凭证号长度为:" + voucher.getVoucherNoLen().toString());
			}
		}
		// 张数
		BigInteger count = new BigInteger(voucherInStockOrderDetail
				.getEndVoucherNo()).subtract(new BigInteger(
				voucherInStockOrderDetail.getStartVoucherNo()));
		int intcount=count.intValue()+1;
		if (intcount < 1) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
					"张数不能小于1!");
		}
		// 插入数据
		voucherInStockOrderDetail
				.setCount(intcount);
		voucherInStockOrderDetailDaoImpl
				.insertVoucherInStockOrderDetail(voucherInStockOrderDetail);
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "凭证入库单明细",
				voucherInStockOrderDetail, SysOperLog.operType_Add,
				"VoucherId:" + voucherInStockOrderDetail.getVoucherId());
	}

	private void insertRecordingOperationLog(PCPSWebInput webInput,
			String logName, VoucherInStockOrderDetail obj, String operType,
			String lastFieldName) throws PCPSApplicationException {
		String logRemark = PCPSUtil.RecordingOperationLog(obj, lastFieldName);
		if (!PCPSUtil.isEmpty(logRemark)) {
			// 插入数据库日志
			SysOperLog sysOperLog = new SysOperLog();
			if (webInput != null && webInput.getOperId() != null) {
				sysOperLog.setOperId(webInput.getOperId());
				sysOperLog.setOperType(operType);
				sysOperLog.setLogName(logName);
				if (webInput != null) {
					sysOperLog.setIpAddress(webInput.getIpAddress());
				}
				if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
					sysOperLog.setIssueCompanyNo(webInput.getOperBranchNo()
							.substring(0, 6));
					sysOperLog.setBranchNo(webInput.getOperBranchNo());
				}
				sysOperLog.setRemark(logRemark);
				sysOperLogDaoImpl.insertSysOperLog(sysOperLog);
			}
		}
	}
}
